package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwazT;
import cn.e4j.modular.crm.service.impl.GwazTServiceImpl;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 法人组织参数设置管理
 * @author liuw
 * @date 2019/08/30
 */
@Controller
@RequestMapping("/crm/gwas100")
public class Gwas100Controller extends BaseController {

    @Autowired
    private GwazTServiceImpl gwazTService;

    /**
     * 跳转法人组织参数设置页面
     */
    @RequiresPermissions("crm:gwas100:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwas100.html";
    }

    /**
     * 查询法人组织参数设置信息列表
     */
    @RequestMapping("/getGwazlist")
    @RequiresPermissions("crm:gwas100:init")
    @ResponseBody
    public ResponseData getGwaelist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwazTService.getGwazlist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取新增参数类型
     */
    @RequestMapping("/getGwaz001")
    @RequiresPermissions("crm:gwas100:init")
    @ResponseBody
    public ResponseData getGwaz001(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto addDataDto = Dtos.newDto();
        addDataDto.put("GWAZ001", "SCC" + (int) ((Math.random() * 9 + 1) * 1000));
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwas100:init")
    @ResponseBody
    public ResponseData add(GwazT gwaz) {
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断编号是否重复
        GwazT theGwazT = gwazTService.getByKey(shiroUser.getDBC(), gwaz.getGWAZ001(), gwaz.getGWAZLEGAL());
        if (theGwazT != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_CODE.getCode(),
                    CrmExceptionEnum.EXISTED_THE_CODE.getMessage());
        }
        gwazTService.add(gwaz);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwas100:init")
    @ResponseBody
    public ResponseData edit(GwazT gwaz) {
        gwazTService.edit(gwaz);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwas100:init")
    @ResponseBody
    public ResponseData del(GwazT gwaz) {
        gwazTService.delete(gwaz);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwas100:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwazT> gwazTList = inDto.getReqList(GwazT.class);
        gwazTService.delDataBatch(gwazTList);
        return SUCCESS_TIP;
    }

}
